/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.client.modular.goods;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.biz.modular.cabinetdoor.service.BizCabinetDoorService;
import vip.xiaonuo.biz.modular.cabinetdoor.vo.ClientCabinetDoor;
import vip.xiaonuo.biz.modular.cabinetgroup.service.BizCabinetGroupService;
import vip.xiaonuo.biz.modular.cabinetgroup.vo.ClientCabinetGroup;
import vip.xiaonuo.biz.modular.cabinetinfo.service.BizCabinetInfoService;
import vip.xiaonuo.biz.modular.cabinetinfo.vo.ClientCabinetInfo;
import vip.xiaonuo.biz.modular.goods.entity.BizGoods;
import vip.xiaonuo.biz.modular.goods.param.ClientGoodsQryParam;
import vip.xiaonuo.biz.modular.goods.param.GoodsQryDto;
import vip.xiaonuo.biz.modular.goods.result.ClientGoodsDetailResult;
import vip.xiaonuo.biz.modular.goods.result.ClientGoodsPageResult;
import vip.xiaonuo.biz.modular.goods.result.ClientGoodsSummaryResult;
import vip.xiaonuo.biz.modular.goods.result.GoodsPageResult;
import vip.xiaonuo.biz.modular.goods.service.BizGoodsService;
import vip.xiaonuo.biz.modular.org.result.ClientOrg;
import vip.xiaonuo.biz.modular.org.service.BizOrgService;
import vip.xiaonuo.biz.modular.scan.param.ScanDto;
import vip.xiaonuo.biz.modular.scan.service.ScanService;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.dev.modular.dict.entity.DevDict;
import vip.xiaonuo.dev.modular.dict.service.DevDictService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物品控制器
 *
 * @author kongxm
 * @date 2022/11/12 21:19
 */
@Api(tags = "物品控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
public class ClientGoodsController {

    @Resource
    private BizGoodsService bizGoodsService;
    @Resource
    private DevDictService devDictService;
    @Resource
    private BizCabinetGroupService bizCabinetGroupService;
    @Resource
    private BizCabinetInfoService bizCabinetInfoService;
    @Resource
    private BizCabinetDoorService bizCabinetDoorService;
    @Resource
    private BizOrgService bizOrgService;

    @Resource
    private ScanService scanService;

    /**
     * @author kongxm
     * @date 2023/02/18 21:19
     */
    @ApiOperationSupport(order = 0)
    @ApiOperation("获取管理模式统计数据")
    @GetMapping("/client/goods/summary/admin")
    public CommonResult<ClientGoodsSummaryResult> queryGoodsAdminSummary(@RequestParam("goodsType") Integer goodsType) {
        return CommonResult.data(bizGoodsService.queryGoodsAdminSummary(goodsType));
    }


    /**
     * @author kongxm
     * @date 2023/02/18 21:19
     * <p>
     * goodsType - 物品类型（分类） 0-常规 1-试剂
     */
    @ApiOperationSupport(order = 0)
    @ApiOperation("获取用户模式统计数据")
    @GetMapping("/client/goods/summary/user")
    public CommonResult<ClientGoodsSummaryResult> queryGoodsUserSummary(@RequestParam("goodsType") Integer goodsType) {
        return CommonResult.data(bizGoodsService.queryGoodsUserSummary(goodsType));
    }

    /**
     * @author kongxm
     * @date 2022/11/12 21:19
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取物品列表")
    @GetMapping("/client/goods/page")
    public CommonResult<IPage<ClientGoodsPageResult>> listForClient(ClientGoodsQryParam param) {
        return CommonResult.data(bizGoodsService.pageForClient(param));
    }

    /**
     * @author kongxm
     * @date 2022/11/12 21:19
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("获取物品详情")
    @GetMapping("/client/goods/detail")
    public CommonResult<ClientGoodsDetailResult> detailForClient(String goodsId, String qrCode) {
        return CommonResult.data(bizGoodsService.getDetailForClient(goodsId, qrCode));
    }

    /**
     * @author kongxm
     * @date 2022/11/12 21:19
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("获取物品分类")
    @GetMapping("/client/goods/category/list")
    public CommonResult<List<DevDict>> listGoodsCategory() {
        DevDict devDict = devDictService.getOne(new LambdaQueryWrapper<DevDict>().eq(DevDict::getDictValue, "GOODS_CATEGORY")
                .last("limit 1"));
        if (devDict == null) return CommonResult.error();
        List<DevDict> dictList = devDictService.list(new LambdaQueryWrapper<DevDict>()
                .select(DevDict::getDictLabel, DevDict::getDictValue)
                .eq(DevDict::getDeleteFlag, "NOT_DELETE")
                .eq(DevDict::getParentId, devDict.getId()));
        return CommonResult.data(dictList);
    }

    /**
     * @author kongxm
     * @date 2022/11/12 21:19
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("获取物品分类-组柜")
    @GetMapping("/client/goods/cabinetgroup/list")
    public CommonResult<List<ClientCabinetGroup>> listCabinetGroupByGoodsForClient() {
        List<ClientCabinetGroup> list = bizCabinetGroupService.listCabinetGroupByGoodsForClient();
        return CommonResult.data(list);
    }

    /**
     * @author kongxm
     * @date 2022/11/12 21:19
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取物品分类-柜子")
    @GetMapping("/client/goods/cabinetinfo/list")
    public CommonResult<List<ClientCabinetInfo>> listCabinetInfoByGoodsForClient() {
        List<ClientCabinetInfo> list = bizCabinetInfoService.listCabinetInfoByGoodsForClient();
        return CommonResult.data(list);
    }

    /**
     * @author kongxm
     * @date 2022/11/12 21:19
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("获取物品分类-柜门")
    @GetMapping("/client/goods/cabinetdoor/list")
    public CommonResult<List<ClientCabinetDoor>> listCabinetDoorByGoodsForClient() {
        List<ClientCabinetDoor> list = bizCabinetDoorService.listCabinetDoorByGoodsForClient();
        return CommonResult.data(list);
    }

    /**
     * @author kongxm
     * @date 2022/11/12 21:19
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation("获取物品分类-组织")
    @GetMapping("/client/goods/org/list")
    public CommonResult<List<ClientOrg>> listOrgByGoodsForClient() {
        List<ClientOrg> list = bizOrgService.listOrgByGoodsForClient();
        return CommonResult.data(list);
    }

    /**
     * @author kongxm
     * @date 2022/11/12 21:19
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation("获取物品图片-因为存储的是base64，所以这里必须单独请求图片，不然页面加载非常慢")
    @GetMapping("/client/goods/img")
    public CommonResult<Map<String, String>> queryGoodsImg(String goodsId) {
        BizGoods bizGoods = bizGoodsService.getById(goodsId);
        if (bizGoods == null) return CommonResult.error("goodsId doesn't exist");
        Map<String, String> map = new HashMap<>(1);
        map.put("goodsImg", bizGoods.getGoodsImg());
        return CommonResult.data(map);
    }

    /**
     * 获取物品信息(扫码用)
     * 入参:scanCode+goodsType
     *
     * @param dto
     * @return
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation("获取物品信息(扫码用)")
    @PostMapping("/client/goods/detailForScan")
    public CommonResult<GoodsPageResult> detailForScan(@RequestBody GoodsQryDto dto) {
        GoodsPageResult bizGoods = bizGoodsService.detailForScan(dto);
        return CommonResult.data(bizGoods);
    }

    /**
     * 批量扫码入库
     * 入参:如下↓（全部必传）
     * {
     *     "goodsType":0,
     *     "cabinetCode":"WFWG008",
     *     "cabinetDoorCode":"door",
     *     "itemList":[
     *         {
     *             "goodsId":"1719625679954235394",
     *             "weigh":"10"
     *         }
     *     ]
     * }
     * @param dto
     * @return
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation("批量扫码入库")
    @PostMapping("/client/goods/scanForSto")
    public CommonResult<Integer> scanForSto(@RequestBody ScanDto dto) {
        int count = scanService.scanForSto(dto);
        if (count > 0) {
            return CommonResult.data(count);
        } else {
            return CommonResult.error();
        }
    }

    /**
     * 扫码物品借出
     * 入参：如下↓ （goodsId必填，weigh非必填）
     * {
     *     "goodsId":"1719981733980393473",
     *     "weigh":"20"
     * }
     * @param dto
     * @return
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation("扫码物品借出")
    @PostMapping("/client/goods/scanForBor")
    public CommonResult<Integer> scanForBor(@RequestBody ScanDto dto) {
        int count = scanService.scanForBor(dto);
        if (count > 0) {
            return CommonResult.data(count);
        } else {
            return CommonResult.error();
        }
    }

    /**
     * 扫码物品归还
     * 入参：如下↓ （weigh非必填，其他必填）
     * {
     *     "goodsId":"1719981733980393473",
     *     "weigh":"15",
     *     "cabinetCode":"W332102",
     *     "cabinetDoorCode":"door"
     * }
     * @param dto
     * @return
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation("扫码物品归还")
    @PostMapping("/client/goods/scanForRtn")
    public CommonResult<Integer> scanForRtn(@RequestBody ScanDto dto) {
        int count = scanService.scanForRtn(dto);
        if (count > 0) {
            return CommonResult.data(count);
        } else {
            return CommonResult.error();
        }
    }
}
